package com.zllkj.mm.module.shopping.fragment

import android.annotation.SuppressLint
import android.view.View
import androidx.viewpager.widget.ViewPager
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayout.OnTabSelectedListener
import com.zllkj.mm.R
import com.zllkj.mm.adapter.FragmentBaseAdapter
import com.zllkj.mm.base.BaseFragment
import com.zllkj.mm.base.BaseTitleFragment
import kotlinx.android.synthetic.main.activity_order_list.view.*

class OrderListFragment : BaseTitleFragment() {
    private val titles = arrayOf("全部", "待付款", "待发货", "待收货", "已完成")
    private val orderFragments = arrayListOf<BaseFragment>()


    @SuppressLint("RestrictedApi")
    override fun loadData() {
        super.loadData()

        setCommonTitle("订单列表")
        setRightBtn("售后", View.OnClickListener {
            addFragment(AfterSaleGoodsListFragment())
        })

        orderFragments.add(OrderListItemFragment(OrderListItemFragment.ORDER_ALL_ORDER))
        orderFragments.add(OrderListItemFragment(OrderListItemFragment.ORDER_WAIT_TO_PAY))
        orderFragments.add(OrderListItemFragment(OrderListItemFragment.ORDER_WAIT_TO_SHIPPING))
        orderFragments.add(OrderListItemFragment(OrderListItemFragment.ORDER_WAIT_TO_RECEIVE))
        orderFragments.add(OrderListItemFragment(OrderListItemFragment.ORDER_FINISH))

        mRootView?.findViewById<TabLayout>(R.id.tb_order_list_tab)?.let {tablayout ->
            titles.mapIndexed { index, s -> mRootView?.tb_order_list_tab?.addTab(tablayout.newTab().setText(s))}
        }
        
        mRootView?.vp_order_list?.adapter= FragmentBaseAdapter(childFragmentManager, orderFragments, titles)
        mRootView?.tb_order_list_tab?.addOnTabSelectedListener(object : OnTabSelectedListener {
            override fun onTabSelected(tab: TabLayout.Tab) {
                mRootView?.vp_order_list?.setCurrentItem(tab.position)
            }
            override fun onTabUnselected(tab: TabLayout.Tab) {}
            override fun onTabReselected(tab: TabLayout.Tab) {}
        })

        mRootView?.vp_order_list?.addOnPageChangeListener(object : ViewPager.SimpleOnPageChangeListener(){

            override fun onPageSelected(p0: Int) {
                mRootView?.tb_order_list_tab?.let {
                    it.setScrollPosition(p0,0f,false)
                }
            }
        })

    }

    override fun getContentLayoutId(): Int {
        return R.layout.activity_order_list
    }


}